package com.nstar.sinaweibo.model
{
	import com.sina.microblog.data.MicroBlogUser;
	
	import flash.data.EncryptedLocalStore;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	
	[Bindable]
	public class MainData extends EventDispatcher
	{
		public var logined:Boolean = false;//是否已经登录
		public var loginData:LoginData = new LoginData();
		public var currentUser:MicroBlogUser;
		public var shownUser:MicroBlogUser;
		public var currentPos:String;
		public var optionsVO:OptionsVO;
		
		
		public var friendTimeLineList:ArrayCollection = new ArrayCollection();
		public var metionsList:ArrayCollection = new ArrayCollection();
		public var commentsList:ArrayCollection = new ArrayCollection();
		public var directMsgList:ArrayCollection = new ArrayCollection();//发出的私信和收到的私信的集合
		public var directMsgReceivedList:ArrayCollection = new ArrayCollection();
		public var directMsgSentList:ArrayCollection = new ArrayCollection();
		public var favoritesList:ArrayCollection = new ArrayCollection(); 
		public var friendsList:ArrayCollection = new ArrayCollection();
		public var followersList:ArrayCollection = new ArrayCollection();
		public var userTimeLineList:ArrayCollection = new ArrayCollection();
		
		
		public var statusCounts:ArrayCollection = new ArrayCollection();
		
		public var faces:ArrayCollection = new ArrayCollection();//表情列表
		public var themes:ArrayCollection = new ArrayCollection();//主题列表
		public var holidays:ArrayCollection = new ArrayCollection();//日期列表
		public var holiday:HolidayVO;//节假日
		public var today:Date;
		
		//记录上一次获取的消息列表ID
		public var lastFriendTimeLineID	:Number = 0;
		public var lastMentionsID		:Number = 0;
		public var lastCommentID		:Number = 0;
		public var lastDirectMessageID	:Number = 0;
		public var lastFollowerID		:Number = 0;
		//新消息数
		public var newFriendTimeLimeCount	:Number = 0;
		public var newMentionsCount			:Number = 0;
		public var newCommentCount			:Number = 0;
		public var newDirectMessageCount	:Number = 0;
		public var newFollwersCount			:Number = 0;
		
		
		
		//加载选项
		public function loadEncryptedOptions():void{
			optionsVO = new OptionsVO();
			
			var firstRun:String = getEncryptedItem("firstRun");
			optionsVO.firstRun = (firstRun == null || firstRun == "Y");
			trace("is first run : "+optionsVO.firstRun);
		}
		
		//保存选项
		public function saveOptions():void{
			trace("save some options");
			saveEncryptedItem("firstRun","N");
			//removeEncryptedItem("firstRun");
		}
		
		//更新首次登录
		public function setFirstRun(value:Boolean):void{
			saveEncryptedItem("firstRun",value?"Y":"N");
			optionsVO.firstRun = value;
		}
		
		//保存加密信息
		private function saveEncryptedItem(key:String,value:String):void{
			var keyBytes:ByteArray = new ByteArray();
			keyBytes.writeUTFBytes(key);
			EncryptedLocalStore.setItem(key,keyBytes);	
		}
		
		//获取加密的信息
		private function getEncryptedItem(key:String):String{
			var keyBytes:ByteArray = EncryptedLocalStore.getItem(key);
			return keyBytes?keyBytes.readUTFBytes(keyBytes.length):null;
		}
		
		//清空加密的信息
		private function removeEncryptedItem(key:String):void{
			EncryptedLocalStore.removeItem(key);
		}
		
		//复位，注销时用
		public function reset():void{
			lastFriendTimeLineID = 0;
			lastMentionsID = 0;
			lastCommentID = 0;
			lastFollowerID = 0;
			
			newFriendTimeLimeCount = 0;
			newMentionsCount = 0;
			newCommentCount = 0;
			newDirectMessageCount = 0;
			newFollwersCount = 0;
			
			friendTimeLineList.removeAll();
			metionsList.removeAll();
			commentsList.removeAll();
			directMsgList.removeAll();
			directMsgReceivedList.removeAll();
			directMsgSentList.removeAll();
			favoritesList.removeAll();
			friendsList.removeAll();
			followersList.removeAll();
			userTimeLineList.removeAll();
			
			logined = false;
			currentUser = null;
			shownUser = null;
		}

	}
}